﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AAA.ResultSet
{
    public class DataGridViewResultSet : AbstractResultSet
    {
        private DataGridView _tblSource;

        public DataGridViewResultSet(DataGridView tblSource)
        {
            _tblSource = tblSource;
        }

        public override bool Load()
        {
            if(_tblSource == null)
            {
                ErrorMessage = "Null DataGridView source";
                return false;
            }

            object[] oValues;
            try
            {
                for (int i = 0; i < _tblSource.Columns.Count; i++)                
                    AddColumn(_tblSource.Columns[i].Name, _tblSource.Columns[i].HeaderText);

                for (int i = 0; i < _tblSource.Rows.Count; i++)
                {
                    oValues = new object[_tblSource.Columns.Count];
                    for (int j = 0; j < _tblSource.Columns.Count; j++)
                        oValues[j] = _tblSource.Rows[i].Cells[j].Value;
                    AddRow(oValues);
                }

                return true;
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message + "," + ex.StackTrace;
            }
            return false;
        }
    }
}
